#ifndef WATCH_H
#define WATCH_H

#include <Arduino.h>
#include <Ticker.h>
#include <WiFiManager.h>
#include <HTTPClient.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <Arduino_JSON.h>
#include <GxEPD2_BW.h>
#include <Wire.h>
#include <Fonts/FreeMonoBold9pt7b.h>
#include "DSEG7_Classic_Bold_53.h"
#include "Display.h"
#include "WatchyRTC.h"
#include "BLE.h"
//#include "bma.h"
#include "config.h"

typedef struct weatherData {
  int8_t temperature;
  int16_t weatherConditionCode;
  bool isMetric;
  String weatherDescription;
  bool external;
} weatherData;

typedef struct watchySettings {
  // Weather Settings
  String cityID;
  String weatherAPIKey;
  String weatherURL;
  String weatherUnit;
  String weatherLang;
  int8_t weatherUpdateInterval;
  // NTP Settings
  String ntpServer;
  int gmtOffset;
  int dstOffset;
  //
  bool vibrateOClock;
} watchySettings;

class InterruptHandler {
public:
  // 构造函数：初始化引脚
  InterruptHandler(int pin);

  // 检查中断是否触发（供外部调用）
  bool isTriggered();
  
private:
  int pin;  // 中断引脚
  volatile bool triggered;  // 中断标志位
public:
  bool isrTriggered;
  Ticker delayTimer;
  static void staticTimerCB(void* arg){
    // 通过参数传递类实例指针
    InterruptHandler* instance = static_cast<InterruptHandler*>(arg);
    instance->handleInterrupt();  // 调用非静态成员函数
  }
  // 静态成员函数（可作为中断回调）
  static void staticISR(void* arg) {
    InterruptHandler* instance = static_cast<InterruptHandler*>(arg);
    if (!instance->isrTriggered){
      instance->isrTriggered = true;
      instance->delayTimer.once_ms(20, staticTimerCB, arg);
    }
  }
  // 非静态成员函数（实际处理中断逻辑）
  void handleInterrupt() {
    noInterrupts();
    if (digitalRead(pin) == HIGH){
      triggered = true;  // 仅设置标志位，避免复杂操作
    }
    isrTriggered = false;
    interrupts();
  }

};

class WatchyLK {
public:
  static WatchyRTC RTC;
  static GxEPD2_BW<WatchyDisplay, WatchyDisplay::HEIGHT> display;
  tmElements_t currentTime;
  watchySettings settings;

public:
  explicit WatchyLK(const watchySettings &s) : settings(s) {} // constructor
  void init(String datetime = "");
  void deepSleepInit(String datetime = "");
  void chargeInit();
  void deepSleep();
  static void displayBusyCallback(const void *);
  float getBatteryVoltage();
  void vibMotor(uint8_t intervalMs = 100, uint8_t length = 20);

  void handleButtonPress(uint64_t btnBit);
  void showMenu(byte menuIndex, bool partialRefresh);
  void showFastMenu(byte menuIndex);
  void showAbout();
  void showBuzz();
  void showAccelerometer();
  void showUpdateFW();
  void showSyncNTP();
  bool syncNTP();
  bool syncNTP(long gmt);
  bool syncNTP(long gmt, String ntpServer);
  void setTime();
  void setupWifi();
  bool connectWiFi();
  weatherData getWeatherData();
  weatherData getWeatherData(String cityID, String units, String lang,
                             String url, String apiKey, uint8_t updateInterval);
  void updateFWBegin();
  void updateChargeState();

  void showWatchFace(bool partialRefresh, bool charging=false);
  virtual void drawWatchFace(bool charging); // override this method for different watch
                                // faces

private:
  //void _bmaConfig();
  static void _configModeCallback(WiFiManager *myWiFiManager);
  static uint16_t _readRegister(uint8_t address, uint8_t reg, uint8_t *data,
                                uint16_t len);
  static uint16_t _writeRegister(uint8_t address, uint8_t reg, uint8_t *data,
                                 uint16_t len);
  // bool mDeepSleepFlag;
  InterruptHandler* mMenuINT;
  InterruptHandler* mBackINT;
  InterruptHandler* mUpINT;
  InterruptHandler* mDownINT;
};

extern RTC_DATA_ATTR int guiState;
extern RTC_DATA_ATTR int menuIndex;
//extern RTC_DATA_ATTR BMA423 sensor;
extern RTC_DATA_ATTR bool WIFI_CONFIGURED;
extern RTC_DATA_ATTR bool BLE_CONFIGURED;




#endif
